

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JPasswordField;


import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.awt.Font;


public class RegistrationUI extends JPanel {
	private JTextField txtUsername;
	private JTextField txtFName;
    private JPasswordField txtPassword;
    private JPasswordField txtRetypePassword;

	private AssetRegistryFrame arFrame;
    DBConnector db = new DBConnector();
    int checker;
    private JTextField txtLName;
    
	/**
	 * Create the panel.
	 */
	public RegistrationUI(AssetRegistryFrame arFrame ) {
		this.arFrame = arFrame;
		setLayout(null);
		
		JLabel lblUsername = new JLabel("Username:");
		lblUsername.setBounds(146, 135, 63, 14);
		add(lblUsername);
		
		JLabel lblPassword = new JLabel("Password:");
		lblPassword.setBounds(146, 220, 63, 14);
		add(lblPassword);
		
		JLabel lblFName = new JLabel("First Name:");
		lblFName.setBounds(443, 141, 89, 14);
		add(lblFName);
		
		txtUsername = new JTextField();
		txtUsername.setBounds(146, 160, 165, 30);
		add(txtUsername);
		txtUsername.setColumns(10);
		
		txtFName = new JTextField();
		txtFName.setBounds(443, 160, 165, 30);
		add(txtFName);
		txtFName.setColumns(10);
		
		JButton btnSubmit = new JButton("Submit");
		btnSubmit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnSubmitActionPerformed(e);
			}
		});
		btnSubmit.setBounds(265, 412, 89, 23);
		add(btnSubmit);
		
		JLabel lblRetypePassword = new JLabel("Re-type Password:");
		lblRetypePassword.setBounds(146, 305, 124, 14);
		add(lblRetypePassword);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnCancelActionPerformed(e);
			}
		});
		btnCancel.setBounds(428, 412, 89, 23);
		add(btnCancel);
		
		txtPassword = new JPasswordField();
		txtPassword.setBounds(146, 245, 165, 30);
		add(txtPassword);
		
		txtRetypePassword = new JPasswordField();
		txtRetypePassword.setBounds(146, 330, 165, 30);
		add(txtRetypePassword);
		
		txtLName = new JTextField();
		txtLName.setColumns(10);
		txtLName.setBounds(443, 245, 165, 30);
		add(txtLName);
		
		JLabel lblLName = new JLabel("Last Name:");
		lblLName.setBounds(443, 220, 89, 14);
		add(lblLName);
		
		JLabel lblHeader = new JLabel("REGISTRATION");
		lblHeader.setFont(new Font("Impact", Font.BOLD, 70));
		lblHeader.setBounds(169, 35, 468, 78);
		add(lblHeader);

	}
	
	private void btnSubmitActionPerformed(ActionEvent e) {
		if(!txtUsername.getText().trim().isEmpty() && !txtPassword.getText().trim().isEmpty()  
				&& !txtRetypePassword.getText().trim().isEmpty() 
				&& !txtFName.getText().trim().isEmpty() && !txtLName.getText().trim().isEmpty()){				// Inputs are not empty
			if(checkUsername()){																// Username doesn't exist
				if(Arrays.equals(txtPassword.getPassword(), txtRetypePassword.getPassword())){	// Passwords match							// Password matches
					createAccount();
					JOptionPane.showMessageDialog(new JFrame(), "Account has been successfully created.");
					arFrame.changePanel(new LoginUI(arFrame));
				}
				else
					DBConnector.alertUser("Passwords doesn't match. Please re-type again.");
			}
			else
				DBConnector.alertUser("Username already exists. Please input another username.");
		}
		else
			DBConnector.alertUser("Please complete the registration form.");
	}
	
	private void createAccount(){
		String query = "INSERT INTO useraccounts(Username, Password, FName, LName, UserType) " +
				" VALUES ('"+txtUsername.getText().trim()+"' , '"+txtPassword.getText().trim()+"' ," +
						" '"+txtFName.getText().trim()+"', '"+txtLName.getText().trim()+"' , 'NEW')";
		try {
			Connection con = DBConnector.getConnection();
			DBConnector.executeUpdate(con, query);
			con.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private boolean checkUsername(){
		String query = "SELECT COUNT(*) AS username FROM useraccounts WHERE username = '"+txtUsername.getText()+"'";
		
		Connection con = DBConnector.getConnection();
		ResultSet rs = DBConnector.executeQuery(con, query);

		try {
			while(rs.next())
			    checker = rs.getInt("username");
		
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		
		if(checker == 1)	// Username existing
			return false;
		else				// Username not existing and ready to use
			return true;
	}
	
	private void btnCancelActionPerformed(ActionEvent e) {
		arFrame.changePanel(new LoginUI(arFrame));
	}
}
